package com.kamistoat.meimeistore.coupon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kamistoat.common.constant.SecKillConstant;
import com.kamistoat.meimeistore.coupon.feign.SecKillFeignService;
import com.kamistoat.meimeistore.coupon.service.SeckillSessionService;
import com.kamistoat.meimeistore.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.kamistoat.meimeistore.coupon.entity.SeckillSkuRelationEntity;
import com.kamistoat.common.utils.PageUtils;
import com.kamistoat.common.utils.R;


/**
 * 秒杀活动商品关联
 *
 * @author kamistoat
 * @email linjianshanke@163.com
 * @date 2021-09-12 16:19:46
 */
@RestController
@RequestMapping("meimeistorecoupon/seckillskurelation")
public class SeckillSkuRelationController {
    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;
    @Autowired
    private SeckillSessionService seckillSessionService;
    @Autowired
    private SecKillFeignService secKillFeignService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("meimeistorecoupon:seckillskurelation:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = seckillSkuRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("meimeistorecoupon:seckillskurelation:info")
    public R info(@PathVariable("id") Long id) {
        SeckillSkuRelationEntity seckillSkuRelation = seckillSkuRelationService.getById(id);

        return R.ok().put("seckillSkuRelation", seckillSkuRelation);
    }

    /**
     * 保存.除了进MySQL，还检查该秒杀是否位于3天以内，如果是，同时在redis进行缓存。
     */
    @RequestMapping("/save")
    //@RequiresPermissions("meimeistorecoupon:seckillskurelation:save")
    public R save(@RequestBody SeckillSkuRelationEntity seckillSkuRelation) {
        R status = seckillSkuRelationService.checkRelation(seckillSkuRelation);
        if((int)status.get("code") == 0){
            seckillSkuRelationService.save(seckillSkuRelation);
            secKillFeignService.delete_ReUp_RedisSessions(getRedisSessionKey(new Long[]{seckillSkuRelation.getPromotionSessionId()}));
            return R.ok();
        }else if((int) status.get("code") == 2){
            SeckillSkuRelationEntity sumRelation = (SeckillSkuRelationEntity) status.get("data");
            seckillSkuRelationService.updateById(sumRelation);
            secKillFeignService.delete_ReUp_RedisSessions(getRedisSessionKey(new Long[]{sumRelation.getPromotionSessionId()}));
            return status;
        }else {
            return status;
        }
    }

    /**
     * 修改.除了进MySQL，还检查该秒杀是否位于3天以内，如果是，同时在redis进行缓存。
     */
    @RequestMapping("/update")
    //@RequiresPermissions("meimeistorecoupon:seckillskurelation:update")
    public R update(@RequestBody SeckillSkuRelationEntity seckillSkuRelation) {
        seckillSkuRelationService.updateById(seckillSkuRelation);
        secKillFeignService.delete_ReUp_RedisSessions(getRedisSessionKey(new Long[]{seckillSkuRelation.getPromotionSessionId()}));
        return R.ok();
    }

    /**
     * 删除.除了进MySQL，还检查该秒杀是否位于3天以内，如果是，同时在redis进行缓存。
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("meimeistorecoupon:seckillskurelation:delete")
    public R delete(@RequestBody Long[] ids) {
        Long promotionSessionId = seckillSkuRelationService.getById(ids[0]).getPromotionSessionId();
        seckillSkuRelationService.removeByIds(Arrays.asList(ids));
        secKillFeignService.delete_ReUp_RedisSessions(getRedisSessionKey(new Long[]{promotionSessionId}));
        return R.ok();
    }


    /**
     * 根据ids制作redis keyList
     *
     * @param ids
     * @return
     */
    public List<String> getRedisSessionKey(@RequestParam Long[] ids) {
        return seckillSessionService.listByIds(Arrays.asList(ids)).stream().map(seckillSessionEntity -> {
            String key = SecKillConstant.SEC_SESSION_PREFIX
                    + seckillSessionEntity.getStartTime().getTime() + "_" + seckillSessionEntity.getEndTime().getTime();
            return key;
        }).collect(Collectors.toList());
    }

}
